library(foreign)
library(gdata)
library(countrycode)
setwd("/Users/danielhill/Documents/cedaw/compliance paper/replication files")

### Polity data
polity.dat<-read.xls(xls="p4v2012.xls")
polity.dat<-data.frame(polity.dat$ccode,polity.dat[,4:5],polity.dat$democ,polity.dat$polity2)
polity.dat<-polity.dat[polity.dat$year>1978&polity.dat$year<2012,]
colnames(polity.dat)[1]<-"ccode"
colnames(polity.dat)[4:5]<-c("democ","polity2")
polity.dat$democ[polity.dat$democ<0]<-NA
polity.dat<-polity.dat[order(polity.dat$ccode,polity.dat$year),]

### GDP/capita and population data
pwt.dat<-read.csv(file="pwt.csv")
pwt.dat$cowcode<-countrycode(pwt.dat$country,origin="country.name",destination="cown")
pwt.dat$cowcode[pwt.dat$country=="Serbia"]<-345
pwt.dat<-pwt.dat[pwt.dat$year>1978,]
pwt.dat<-pwt.dat[!is.na(pwt.dat$cowcode),]
pwt.dat<-pwt.dat[,3:6]
pwt.dat<-pwt.dat[order(pwt.dat$cowcode,pwt.dat$year),]

### ratification data
rat.dat<-read.csv(file="cedaw_rat_ccodes.csv")
rat.dat<-rat.dat[rat.dat$year>1978&rat.dat$year<2012,]
rat.dat$cowcode[rat.dat$cowcode==260&rat.dat$year>1989]<-255
rat.dat$cowcode[rat.dat$cowcode==315&rat.dat$year>1992]<-316

### reservations data
res.dat<-read.csv(file="CEDAW_res.csv")
res.dat$cowcode<-countrycode(res.dat$country,origin="country.name",destination="cown")
res.dat<-res.dat[order(res.dat$cowcode),]
res.not.29<-res.dat$cowcode[res.dat$not_29==1]
res.29<-res.dat$cowcode[res.dat$only_29==1]

## use reservations data to created qualified ratification status variables
rat.dat$res_not_29<-as.numeric(rat.dat$cowcode %in% res.not.29)
rat.dat$res_29<-as.numeric(rat.dat$cowcode %in% res.29)
# ratification w/ no reservations
rat.dat$rat_no_res<-ifelse(rat.dat$ratification==1&rat.dat$res_not_29==0&rat.dat$res_29==0,1,0)
# ratification w/ no reservations that affect primary obligations (ignore cases w/ only Article 29 reservations)
rat.dat$rat_no_primary_res<-ifelse(rat.dat$ratification==1&rat.dat$res_not_29==0,1,0)

### compliance data
comply.dat<-read.csv(file="cedaw_compliance_rep.csv")

comply.dat<-comply.dat[comply.dat$year>1978,]
comply.dat<-data.frame(comply.dat[,1:2],comply.dat[,6:7])

### merge
rat_comply.dat<-merge(rat.dat,comply.dat,by.x=c("cowcode","year"),by.y=c("ccode","year"),all.x=T,all.y=T)

### fill in ratification variable for CEDAW non-signatories 
rat_comply.dat$ratification[rat_comply.dat$cowcode==520|rat_comply.dat$cowcode==625|rat_comply.dat$cowcode==630|rat_comply.dat$cowcode==955]<-0
rat_comply.dat$signature[rat_comply.dat$cowcode==520|rat_comply.dat$cowcode==625|rat_comply.dat$cowcode==630|rat_comply.dat$cowcode==955]<-0
rat_comply.dat$rat_no_res[rat_comply.dat$cowcode==520|rat_comply.dat$cowcode==625|rat_comply.dat$cowcode==630|rat_comply.dat$cowcode==955]<-0
rat_comply.dat$rat_no_primary_res[rat_comply.dat$cowcode==520|rat_comply.dat$cowcode==625|rat_comply.dat$cowcode==630|rat_comply.dat$cowcode==955]<-0

### merge with Polity

## change Polity ccode to COW for Germany, Yugoslavia/Serbia, USSR/Russia, Ethiopia, Vietnam
polity.dat$ccode[polity.dat$ccode==260&polity.dat$year>1990]<-255
polity.dat$ccode[polity.dat$ccode==347&polity.dat$year>1991&polity.dat$year<2006]<-345
polity.dat$ccode[polity.dat$ccode==342&polity.dat$year>2005]<-345
polity.dat$ccode[polity.dat$ccode==364]<-365
polity.dat$ccode[polity.dat$ccode==529]<-530
polity.dat$ccode[polity.dat$ccode==818]<-816

rat_comply_polity.dat<-merge(rat_comply.dat,polity.dat,by.x=c("cowcode","year"),by.y=c("ccode","year"),all.x=T,all.y=T)

### merge w/ GDP/capita data
pwt.dat$cowcode[pwt.dat$cowcode==255&pwt.dat$year<1990]<-260
pwt.dat$cowcode[pwt.dat$cowcode==316&pwt.dat$year<1993]<-315
rat_comply_polity_pwt.dat<-merge(rat_comply_polity.dat,pwt.dat,by=c("cowcode","year"),all.x=T,all.y=T)

### keep what I need
the.dat<-data.frame(rat_comply_polity_pwt.dat[,1:2],rat_comply_polity_pwt.dat[,5],rat_comply_polity_pwt.dat[,9:12],rat_comply_polity_pwt.dat[,14:17])
colnames(the.dat)[3]<-"ratify"
the.dat<-the.dat[!is.na(the.dat$latent.mean),]
the.dat$lngdp<-log(the.dat$rgdpch)
the.dat$lnpop<-log(the.dat$POP)

### create polity dummies, interactions

democ.vals<-sort(unique(the.dat$democ))
democ.dummies<-matrix(nrow=nrow(the.dat),ncol=length(democ.vals))
democ.dummies<-data.frame(democ.dummies)
rat.democ.interacts<-matrix(nrow=nrow(the.dat),ncol=length(democ.vals)*3)
rat.democ.interacts<-data.frame(rat.democ.interacts)

ratnores.democ.interacts<-matrix(nrow=nrow(the.dat),ncol=length(democ.vals))
ratnores.democ.interacts<-data.frame(ratnores.democ.interacts)

ratnoprimaryres.democ.interacts<-matrix(nrow=nrow(the.dat),ncol=length(democ.vals))
ratnoprimaryres.democ.interacts<-data.frame(ratnoprimaryres.democ.interacts)

for (i in 1:length(democ.vals)){
	democ.dummies[,i]<-as.numeric(the.dat$democ==democ.vals[i])
	tmp<-paste("democ",democ.vals[i],sep="_")
	colnames(democ.dummies)[i]<-tmp

	rat.democ.interacts[,i]<-the.dat$ratify*democ.dummies[,i]
	tmp<-paste("ratify_democ",democ.vals[i],sep="_")
	colnames(rat.democ.interacts)[i]<-tmp	

	rat.democ.interacts[,i+length(democ.vals)]<-the.dat$rat_no_primary_res*democ.dummies[,i]
	tmp<-paste("rat_no_primary_res_democ",democ.vals[i],sep="_")
	colnames(rat.democ.interacts)[i+length(democ.vals)]<-tmp	

	rat.democ.interacts[,i+length(democ.vals)*2]<-the.dat$rat_no_res*democ.dummies[,i]
	tmp<-paste("rat_no_res_democ",democ.vals[i],sep="_")
	colnames(rat.democ.interacts)[i+length(democ.vals)*2]<-tmp	
	}

### time counter
the.dat$time<-the.dat$year-1978

### csv file
democ.dat<-data.frame(the.dat,democ.dummies,rat.democ.interacts)
write.csv(democ.dat,file="democ.dat.rep.csv",row.names=F)

